// ================================================================================
// 		File: PartyVolunteerQuotation.cs
// 		Desc: 
//  
// 		Called by:   
//               
// 		Auth: chenwenzhi（）
// 		Date: 2016-04-01 22:01
// ================================================================================
// 		Change History
// ================================================================================
// 		Date:		Author:				Description:
// 		--------	--------			-------------------
//    
// ================================================================================
// 
// ================================================================================
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace ZHDJ.Data.Models.Center
{
    /// <summary>
    /// 志愿者心语
    /// </summary>   
    public class PartyVolunteerQuotation : EntityBase
    {
        #region 属性


        /// <summary>
        /// 志愿活动ID
        /// </summary>
        public string ActivitieID { get; set; }

        /// <summary>
        /// 心语(50字)
        /// </summary>
        public string Content { get; set; }

        /// <summary>
        /// 创建用户
        /// </summary>
        public string CreateBy { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime? CreateTime { get; set; }

        /// <summary>
        /// 修改用户
        /// </summary>
        public string ModifyBy { get; set; }

        /// <summary>
        /// 更新时间
        /// </summary>
        public DateTime? ModifyTime { get; set; }

        #endregion

        #region 查询管理

        public static DataSet GetPersonalGridData(int pageIndex, int pageSize, string content = "",string currentmemberID = "")
        {
            List<MySqlParameter> parameters = new List<MySqlParameter>()
            {
                new MySqlParameter(){DbType = DbType.Int32,ParameterName = "Begin",Value = (pageIndex)*pageSize},
                new MySqlParameter(){DbType = DbType.Int32,ParameterName = "PageSize",Value = pageSize}
            };


            StringBuilder sb = new StringBuilder();
            sb.Append(@"select SQL_CALC_FOUND_ROWS ID,Content,CreateTime from tblPartyVolunteerQuotation where 1=1");

            if (!string.IsNullOrEmpty(content))
            {
                sb.Append(" and Content like ?Content ");
                parameters.Add(
                    new MySqlParameter() { DbType = DbType.String, ParameterName = "Content", Value = string.Format("%{0}%", content) });
            }
            sb.AppendFormat(" and CreateBy='{0}'", currentmemberID);
            sb.Append(" order by CreateTime desc LIMIT ?Begin ,?PageSize;SELECT FOUND_ROWS();");
            return MySqlHelper.ExecuteDataSet(sb.ToString(), parameters.ToArray());
        }

        public static DataSet GetBranchGridData(int pageIndex, int pageSize, string content, string branchId, string currentBranchID = "")
        {
            List<MySqlParameter> parameters = new List<MySqlParameter>()
            {
                new MySqlParameter(){DbType = DbType.Int32,ParameterName = "Begin",Value = (pageIndex)*pageSize},
                new MySqlParameter(){DbType = DbType.Int32,ParameterName = "PageSize",Value = pageSize}
            };


            StringBuilder sb = new StringBuilder();
            sb.Append(@"select SQL_CALC_FOUND_ROWS quotation.ID,Content,quotation.CreateTime,UserName,BranchName from tblPartyVolunteerQuotation quotation
left join tblpartyorgmember member on quotation.CreateBy=member.LoginID
left join tblpartyorgbranch branch on member.BranchID=branch.BranchID where 1=1");

            if (!string.IsNullOrEmpty(content))
            {
                sb.Append(" and Content like ?Content ");
                parameters.Add(
                    new MySqlParameter() { DbType = DbType.String, ParameterName = "Content", Value = string.Format("%{0}%", content) });
            }
            if (!string.IsNullOrEmpty(branchId))
            {
                sb.Append(" and member.BranchID in (select childbranchid from tblpartyorgbranchrelation where BranchID=?BranchID)");
                parameters.Add(
                    new MySqlParameter() { DbType = DbType.String, ParameterName = "BranchID", Value = branchId });
            }
            else
            {
                sb.AppendFormat(" and member.BranchID='{0}'", currentBranchID);
            }

            sb.Append(" order by quotation.CreateTime desc LIMIT ?Begin ,?PageSize;SELECT FOUND_ROWS();");
            return MySqlHelper.ExecuteDataSet(sb.ToString(), parameters.ToArray());
        }

        public static DataSet GetQuotationTitle(string branchId, string countFilter = "")
        {
            StringBuilder sb = new StringBuilder();
            List<MySqlParameter> list = new List<MySqlParameter>();
            sb.Append("select ID,Content from tblpartyvolunteerquotation where 1=1");
            //if (!string.IsNullOrEmpty(branchId))
            //{
            //    sb.Append(" and ActivitieID in (select ID from tblpartyvolunteeractivitie a where a.BranchID =?BranchID)");
            //    list.Add(new MySqlParameter() { DbType = DbType.String, ParameterName = "BranchID", Value = branchId });
            //}
            sb.AppendFormat(" order by CreateTime desc {0};SELECT FOUND_ROWS();", countFilter);

            return MySqlHelper.ExecuteDataSet(sb.ToString(), list.ToArray());
        }

        public static DataSet GetMoreQuotationTitle(int pageSize, int pageIndex, string branchId="")
        {
            StringBuilder sb = new StringBuilder();
            List<MySqlParameter> parameters = new List<MySqlParameter>();

            sb.Append(@"select SQL_CALC_FOUND_ROWS a.ID,a.Content,a.CreateTime from tblpartyvolunteerquotation a where 1=1 ");

            //sb.Append("select SQL_CALC_FOUND_ROWS a.ID,a.Content,a.CreateTime from tblpartyvolunteerquotation a " +
            //          " inner join tblpartyvolunteeractivitie v on(v.id =a.ActivitieID) where 1=1 " +
            //          " ");
            //if (!string.IsNullOrEmpty(branchId))
            //{
            //    sb.Append(" and v.BranchID=?BranchID ");
            //    parameters.Add(new MySqlParameter() { DbType = DbType.String, ParameterName = "BranchID", Value = branchId });
            //}

            sb.Append(" order by CreateTime desc ");
            if (pageIndex > -1)
            {
                sb.AppendFormat(" LIMIT {0} ,{1}; SELECT FOUND_ROWS(); ", pageIndex, pageSize);
            }

            return MySqlHelper.ExecuteDataSet(sb.ToString(), parameters.ToArray());
        }
        #endregion
    }
}
